package com.soulless.mybatis.ext;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.AbstractJavaGenerator;
import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3SimpleImpl;
import org.mybatis.generator.config.PropertyRegistry;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zenglei
 * @Description 功能描述
 * @date 2020/11/17 14:32
 */
public class GeneratorMapperContext extends IntrospectedTableMyBatis3SimpleImpl {

    @Override
    public List<GeneratedJavaFile> getGeneratedJavaFiles() {
        List<GeneratedJavaFile> answer = new ArrayList<>();

        for (AbstractJavaGenerator javaGenerator: javaModelGenerators) {
            List<CompilationUnit> compilationUnits = javaGenerator.getCompilationUnits();
            for (CompilationUnit compilationUnit: compilationUnits) {
                GeneratedJavaFile gif = new GeneratedJavaFile(
                        compilationUnit,
                        context.getJavaModelGeneratorConfiguration()    // 获取源文件生成javamodel文件对象
                                .getTargetProject(),
                        context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
                        context.getJavaFormatter());
                answer.add(gif);
            }
        }

//        for (AbstractJavaGenerator javaGenerator: clientGenerators) {
//            List<CompilationUnit> compilationUnits = javaGenerator.getCompilationUnits();
//            for (CompilationUnit compilationUnit: compilationUnits) {
//                GeneratedJavaFile gif = new GeneratedJavaFile(
//                        compilationUnit,
//                        context.getJavaClientGeneratorConfiguration() // 获取源文件生成java文件对象
//                        .getTargetProject(),
//                        context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
//                        context.getJavaFormatter());
//                answer.add(gif);
//            }
//        }
//        return answer;

        for (AbstractJavaGenerator javaGenerator : clientGenerators) {
            List<CompilationUnit> compilationUnits = javaGenerator.getCompilationUnits();
            for (CompilationUnit compilationUnit : compilationUnits) {
                // 将源文件转为接口
                Interface interfazz = (Interface) compilationUnit;
                String targetProject = context.getJavaClientGeneratorConfiguration().getTargetProject();
                String targetPackage = context.getJavaClientGeneratorConfiguration().getTargetPackage();
                String domainObjectName = interfazz.getType().getShortName();
                // 判断是否存在该文件
                if (isNotExists(targetProject + "." + targetPackage, domainObjectName + ".java")) {
                    Method method = new Method("find");
//                    method.setVisibility(method.getVisibility());
                    method.setReturnType(new FullyQualifiedJavaType("List<" + domainObjectName + ">"));
                    method.addParameter(new Parameter(new FullyQualifiedJavaType(domainObjectName),
                            domainObjectName.substring(0, 1).toLowerCase() + domainObjectName.substring(1)));
                    interfazz.addMethod(method);
                    GeneratedJavaFile gjf = new GeneratedJavaFile(
                            interfazz,
                            targetProject,
                            context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
                            context.getJavaFormatter());
                    answer.add(gjf);
                }
            }
        }
        return answer;
    }

    /**
     * 判断文件是否存在，不存在返回true
     *
     * @param path
     * @param fileName
     * @return
     */
    private boolean isNotExists(String path, String fileName) {
        String bn = System.getProperty("user.dir");
        String file = bn + "." + path;
        String pathName = file.replace(".", "/").replace("\\", "/") + "/" + fileName;
        File f = new File(pathName);
        return !f.exists();
    }

    private void getAttribute(XmlElement xmlElement, String name, String value) {
        xmlElement.addAttribute(new Attribute(name, value));
    }
}
